package com.yzc.mihuyou.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yzc.mihuyou.entity.*;
import com.yzc.mihuyou.mapper.*;
import com.yzc.mihuyou.service.InvitationService;
import com.yzc.mihuyou.service.TopicService;
import com.yzc.mihuyou.util.CommentUtils;
import com.yzc.mihuyou.util.DateUtil;
import com.yzc.mihuyou.util.TokenProccessor;
import com.yzc.mihuyou.vo.JsonResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;

import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.*;

/**
 * <p>
 * 帖子表 	 前端控制器
 * </p>
 *
 * @author 易紫诚
 * @since 2022-04-28
 */
@RestController
@RequestMapping("/mihuyou/invitation")
public class InvitationController {

    @Autowired
    InvitationService invitationService;
    @Autowired
    IngvseMapper IngvseMapper;
    @Autowired
    IccompMapper iccompMapper;
    @Autowired
    StrategyMapper strategyMapper;
    @Autowired
    TopicService topicService;
    @Autowired
    TokenProccessor tokenProccessor;

    @PostMapping("tiezi")
    public  String text(@RequestPart("photos") MultipartFile photos){
        invitationService.importProfession(photos);
        return "";
    }

    @GetMapping("yue/{id}")
    public JsonResult serle(@PathVariable("id") int id,Integer type, PageWuer pageWuer, HttpServletRequest request){
        System.out.println(pageWuer.getPerv());
        Long l=0L;
        String token = request.getHeader("token");
        User user=null;
        if(token!=null){
            user= tokenProccessor.verify(token);
        }
        if(user!=null){
            l=user.getUserId();
        }
        if(type==null){
            type=0;
        }
        if(pageWuer.getSize()==null){
            pageWuer.setSize(10L);
        }
        if(pageWuer.getCurrent()==null){
            pageWuer.setCurrent(0L);
        }
        if(id==5 && type==3){
            System.out.println("话题查询");
           List<Strategy> strategies=strategyMapper.selectList(null);
           List listRs=new ArrayList();
           for (int s=0;s<strategies.size();s++){
               Map<String,Object> map=new HashMap<>();
               List<String> list=CommentUtils.strUwe(strategies.get(s).getTopicId());
               List<Topic> list1 = new ArrayList<>();
               for (int ik=0;ik<list.size();ik++){
                   if(!list.get(ik).equals("")){
                       list1.add(ik,topicService.selTope(Integer.parseInt(list.get(ik))));
                   }
               }
               map.put("tope", list1);
               map.put("ins",strategies.get(s).getStrategyName());
               map.put("rt",true);
               map.put("sizePow",3);
               listRs.add(listRs.size(),map);
           }
           Map<String,Object> map=new HashMap<>();
           map.put("wud",listRs);
            return  new JsonResult(200,"查询成功",map);
        }

       return  invitationService.serInver(id,type,l,pageWuer);
    }

    @GetMapping("arrIns/{id}")
    public  JsonResult ser(@PathVariable("id") int id, HttpServletRequest request){
        Long l=0L;
        String token = request.getHeader("token");
        User user=null;
        if(token!=null){
            user= tokenProccessor.verify(token);
        }
        if(user!=null){
            l=user.getUserId();
        }
       Map<String,Object> map=invitationService.serOne(id,l);
        if(map==null){
            return  new JsonResult(404,"帖子已被删除或不存在");
        }
        return new JsonResult(200,"查询成功",map);
    }

    @PostMapping("addInvw")
    public  JsonResult addInvw(Invitation invitation,String obj,Integer iccon){
        int inviId=invitationService.addInse(invitation);
        IngvseMapper.insert(new Ingvse(inviId,obj));
        if(iccon!=null){
            iccompMapper.insert(new Iccomp(inviId,iccon));
        }
        return new JsonResult(200,"添加成功");
    }

    @PutMapping("upIns")
    public  JsonResult upIns(Invitation invitation,String obj,Integer iccon){
        invitationService.upInse(invitation);
        QueryWrapper<Ingvse> queryWrapper2=new QueryWrapper<>();
        queryWrapper2.eq("invition_id",invitation.getInvitationId());
        IngvseMapper.update(new Ingvse(invitation.getInvitationId(),obj),queryWrapper2);
        QueryWrapper<Iccomp> queryWrapper=new QueryWrapper<>();
        if(iccon==null){
            queryWrapper.eq("ivitaticon",invitation.getInvitationId());
            iccompMapper.delete(queryWrapper);
        }else {
            queryWrapper.eq("ivitaticon",invitation.getInvitationId());
            Iccomp iccomp=new Iccomp();
            iccomp.setCommentsId(iccon);
            iccompMapper.update(iccomp,queryWrapper);
        }
        return new JsonResult(200,"修改成功");
    }

    @DeleteMapping("delIns")
    public  JsonResult delIns(Integer id){
        if(id==null){
            return new JsonResult(200,"请选中要删除的帖子");
        }
        int k=invitationService.delInse(id);
        if(k==0){
            return new JsonResult(200,"删除失败");
        }
        return new JsonResult(200,"删除成功");
    }

    @PostMapping("/suePre/{id}")
    public  JsonResult suePre(@PathVariable("id") int id,PageWuer pageWuer,String item,HttpServletRequest request){
        Long l=0L;
        String token = request.getHeader("token");
        User user=null;
        if(token!=null){
            user= tokenProccessor.verify(token);
        }
        if(user!=null){
            l=user.getUserId();
        }
        if(pageWuer.getSize()==null){
            pageWuer.setSize(5L);
        }
        if(pageWuer.getCurrent()==null){
            pageWuer.setCurrent(0L);
        }
        return  invitationService.suePre(id, l, pageWuer,DateUtil.StrToDate(item));
    }

}

